package b3.scene.home
{
	import as3isolib.core.as3isolib_internal;
	import as3isolib.display.scene.*;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.graphics.Stroke;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.*;
	
	import com.greensock.*;
	
	use namespace as3isolib_internal;
	
	public class HighlightGrid extends IsoGrid
	{
		private var color:uint;
		private var highlights:Array;
		private var timer:Timer;
		
		public function HighlightGrid()
		{
			super();
			highlights = [new Pt(0,0)];
			color = 0xCCCCCC;
			container.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			container.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddToStage(event:Event):void
		{
			startTimer();
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			stopTimer();
		}
		
		private function startTimer():void
		{
			if (timer == null)
			{
				timer = new Timer(1400);
				timer.addEventListener(TimerEvent.TIMER, onTick);
				timer.start();
			}
		}
		
		private function stopTimer():void
		{
			if (timer != null)
			{
				timer.removeEventListener(TimerEvent.TIMER, onTick);
				timer.stop();
			}
			timer = null;
		}
		
		private function onTick(event:Event):void
		{
			var timeline:TimelineLite = new TimelineLite();
			timeline.append(new TweenLite(container, 0.6, {alpha: 0.4}));   
			timeline.append(new TweenLite(container, 0.6, {alpha: 1}));
		}
		
		public function setHighlight(pts:Array, color:uint):void
		{
			highlights = pts;
			this.color = color;
			drawGeometry();
		}
		
		public function getHighlights():Array
		{
			return highlights;
		}
		
		override protected function drawGeometry():void
		{
			var g:Graphics = mainContainer.graphics;
			g.clear();
			
			var stroke:Stroke = new Stroke(2, color);
			stroke.apply(g);
			
			
			var i:int;
			var cSize:Number = cellSize - 12;
			for (i = 0; i< highlights.length; i++)
			{
				var point:Point = highlights[i];
				var tempPt:Pt = new Pt(point.x * cellSize + 6, point.y * cellSize + 6);
				//画高亮部分
				stroke = new Stroke(5, color);
				stroke.apply(g);
				var pt:Pt = new Pt();
				pt = IsoMath.isoToScreen(new Pt(tempPt.x, tempPt.y));
				g.moveTo(pt.x, pt.y);
	
				pt = IsoMath.isoToScreen(new Pt(tempPt.x + cSize, tempPt.y));
				g.lineTo(pt.x, pt.y);
				
				pt = IsoMath.isoToScreen(new Pt(tempPt.x + cSize, tempPt.y + cSize));
				g.lineTo(pt.x, pt.y);
				
				pt = IsoMath.isoToScreen(new Pt(tempPt.x, tempPt.y + cSize));
				g.lineTo(pt.x, pt.y);
				
				pt = IsoMath.isoToScreen(new Pt(tempPt.x, tempPt.y));
				g.lineTo(pt.x, pt.y);
				
			}
			
			g.endFill();
		}
	}
}